package client

import (
	"GraduationProject/public/models"
	"GraduationProject/public/params"
	"GraduationProject/public/rpc_func"
	"GraduationProject/public/tools"
	"fmt"
	"log"
	"net/rpc"
)

var EngineClient *rpc.Client

func init() {
	var err error
	EngineClient, err = rpc.Dial("tcp", tools.Conf.Get("engine_host"))
	fmt.Println("engine_host", tools.Conf.Get("engine_host"))
	if err != nil {
		panic(err)
	}
}

func PushResult(client *rpc.Client, result params.Result) (response params.Message) {
	if err := client.Call(rpc_func.EnginePushResult, result, &response); err != nil {
		log.Printf("PushResult error: %s\n", err.Error())
		response.Status = -1
		response.Message = err.Error()
	}
	return
}

func NewWorkerID(client *rpc.Client, worker models.Worker) (response params.Message) {
	if err := client.Call(rpc_func.EngineNewWorkerID, worker, &response); err != nil {
		log.Printf("NewWorkerID error: %s\n", err.Error())
		response.Status = -1
		response.Message = err.Error()
	}
	return
}
